package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.EmployeeQueryObject;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;
    @Autowired
    IDepartmentService departmentService;
    @Autowired
    IRoleService roleService;
    @RequestMapping("/list")
    @RequiredPermission({"员工列表","employee:list"})
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model){
        model.addAttribute("departments",departmentService.listAll());
        model.addAttribute("result",employeeService.query(qo));
        return "employee/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){
        if(id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("entity",employee);
        }
        model.addAttribute("roles",roleService.listAll());
        model.addAttribute("departments",departmentService.listAll());
        return "employee/input";
    }

    @RequestMapping("saveOrUpdate")
    @RequiredPermission({"员工新增或编辑","employee:saveOrUpdate"})
    public String saveOrUpdate(Employee employee,Long[] ids){
        employeeService.saveOrUpdate(employee,ids);
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/delete")
    @RequiredPermission({"员工删除","employee:delete"})
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list.do";
    }
}
